//
//  ZLPlateNumberInputView.m
//  ZLPlateNumberInputView
//
//  Created by 科大物联 on 2023/6/28.
//

#import "ZLPlateNumberInputView.h"
#import <Masonry/Masonry.h>

@interface ZLPlateNumberInputViewCell : UICollectionViewCell
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *energyLabel;
@end

@implementation ZLPlateNumberInputViewCell
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.contentView.layer.borderWidth = 0.5;
        self.contentView.layer.cornerRadius = 3;
        [self.contentView addSubview:self.titleLabel];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsZero);
        }];
        [self.contentView addSubview:self.energyLabel];
        [self.energyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsZero);
        }];
    }
    return self;
}
- (void)normalStyle {
    self.energyLabel.hidden = YES;
    self.contentView.layer.borderColor = [UIColor colorWithRed:208/255.0 green:230/255.0 blue:254/255.0 alpha:1.0].CGColor;
    self.contentView.backgroundColor = [UIColor colorWithRed:242/255.0 green:247/255.0 blue:255/255.0 alpha:1.0];
}
- (void)selectStyle {
    self.energyLabel.hidden = YES;
    self.contentView.layer.borderColor = [UIColor colorWithRed:9/255.0 green:87/255.0 blue:244/255.0 alpha:1.0].CGColor;
    self.contentView.backgroundColor = [UIColor colorWithRed:242/255.0 green:247/255.0 blue:255/255.0 alpha:1.0];
}
- (void)energyStyle {
    self.energyLabel.hidden = NO;
    self.contentView.layer.borderColor = [UIColor colorWithRed:158/255.0 green:227/255.0 blue:210/255.0 alpha:1.0].CGColor;
    self.contentView.backgroundColor = [UIColor colorWithRed:236/255.0 green:250/255.0 blue:246/255.0 alpha:1.0];
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _titleLabel.textColor = [UIColor colorWithRed:20/255.0 green:33/255.0 blue:57/255.0 alpha:1.0];
        _titleLabel.font = [UIFont boldSystemFontOfSize:18];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}
- (UILabel *)energyLabel {
    if (!_energyLabel) {
        _energyLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _energyLabel.textColor = [UIColor colorWithRed:60/255.0 green:197/255.0 blue:165/255.0 alpha:1.0];
        _energyLabel.font = [UIFont boldSystemFontOfSize:9];
        _energyLabel.textAlignment = NSTextAlignmentCenter;
        _energyLabel.numberOfLines = 0;
        _energyLabel.text = @"新\n能\n源";
    }
    return _energyLabel;
}
@end

@interface ZLPlateNumberInputView()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
@end

@implementation ZLPlateNumberInputView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        [self addGestureRecognizer:tap];
        [self addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsZero);
        }];
    }
    return self;
}

- (void)layoutSubviews {
    [self.collectionView reloadData];
}

- (void)setEditing:(BOOL)editing {
    _editing = editing;
    [self.collectionView reloadData];
}

#pragma mark - action

- (void)tapAction {
    self.editing = YES;
    if (self.tapBlock) {
        self.tapBlock();
    }
}

#pragma mark - setter

- (void)setPlateNumber:(NSString *)plateNumber {
    if (plateNumber.length > 8) {
        _plateNumber = [plateNumber substringToIndex:8];
    } else {
        _plateNumber = plateNumber;
    }
    self.editing = YES;
    [self.collectionView reloadData];
}

#pragma mark - delegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 8;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ZLPlateNumberInputViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"ZLPlateNumberInputViewCell" forIndexPath:indexPath];
    if (indexPath.row < self.plateNumber.length) {
        cell.titleLabel.text = [self.plateNumber substringWithRange:NSMakeRange(indexPath.row, 1)];
    } else {
        cell.titleLabel.text = @"";
    }
    if (self.plateNumber.length == 8) {
        [cell normalStyle];
        return cell;
    }
    if (self.plateNumber.length == indexPath.row && self.editing) {
        [cell selectStyle];
        return cell;
    }
    if (indexPath.row == 7) {
        [cell energyStyle];
        return cell;
    }
    [cell normalStyle];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((self.frame.size.width - 56) / 8.0, self.frame.size.height);
}

#pragma mark - init

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumInteritemSpacing = 8;
        layout.minimumLineSpacing = 8;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor clearColor];
        [_collectionView registerClass:[ZLPlateNumberInputViewCell class] forCellWithReuseIdentifier:@"ZLPlateNumberInputViewCell"];
        _collectionView.userInteractionEnabled = NO;
    }
    return _collectionView;
}

@end
